<template>
  <el-drawer
    v-model="dialogFormVisible"
    :title="dialogTitle"
    size="1000"
    @close="handleClose"
  >
    <el-form
      :model="formData"
      ref="formRef"
      :rules="rules"
      label-position="right"
      label-width="80px"
      size="default"
      :scroll-to-error="true"
      :show-message="false"
    >
      <el-form-item
        label="产品名称"
        prop="part_name"
        class="required label-right-align"
        label-width="120px"
      >
        <el-input
          v-model="formData.part_name"
          type="text"
          clearable
          :style="{ width: '300px' }"
        ></el-input>
      </el-form-item>
      <el-form-item
        label="产品类目"
        prop="category"
        class="required label-right-align"
        label-width="120px"
      >
        <el-select v-model="formData.category" :style="{ width: '300px' }">
          <el-option
            v-for="item in category"
            :key="item.value"
            :label="item.label"
            :value="item.label"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item
        label="施工价格"
        prop="sort"
        class="label-right-align"
        label-width="120px"
      >
        <el-input
          v-model="formData.price"
          type="text"
          :style="{ width: '300px' }"
        ></el-input>
      </el-form-item>
      <el-form-item
        label="质保年限"
        prop="period"
        class="label-right-align"
        label-width="120px"
      >
        <el-input-number
          v-model="formData.period"
          controls-position="right"
          :min="0"
          :max="99"
          :precision="0"
          :step="1"
        ></el-input-number>
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="dialogClose">关闭</el-button>
        <el-button type="primary" @click="submitForm"> 提交 </el-button>
      </div>
    </template>
  </el-drawer>
</template>

<script setup lang="ts">
import { ref, reactive, defineExpose, defineEmits, onMounted } from "vue";
import { ElMessage } from "element-plus";
import { addApi, editApi, infoApi } from "@/api/carfilm";
const dialogFormVisible = ref(false);
const formRef = ref<any>(null);
const dialogTitle = ref("");

defineProps({
  category: {
    type: Array,
    default: [] as any[],
  },
});
const formData = ref({
  id: 0,
  category: 0,
  part_name: "",
  price: "",
  period: 1,
  status: true,
});

const rules = reactive({
  category: [
    {
      required: true,
      message: "请选择产品类目",
    },
  ],
  part_name: [
    {
      required: true,
      message: "请输入产品名称",
    },
  ],
});

const emits = defineEmits(["getTable"]);
function addSave() {
  addApi(formData.value)
    .then((res) => {
      ElMessage.success("添加成功");
      emits("getTable");
      dialogClose();
    })
    .catch((err) => {});
}
function editSave() {
  editApi(formData.value)
    .then((res) => {
      ElMessage.success("编辑成功");
      emits("getTable");
      dialogClose();
    })
    .catch((err) => {});
}
function getInfo(id: number) {
  infoApi(id)
    .then((res: any) => {
      if (res.code == 1) {
        formData.value = res.data;
      } else {
        ElMessage.error(res.msg);
      }
    })
    .catch((err) => {});
}

function submitForm() {
  formRef.value.validate().then(() => {
    if (formData.value.id == 0) {
      addSave();
    } else {
      editSave();
    }
  });
}

const dialogClose = () => {
  dialogFormVisible.value = false;
};

const dialogOpen = (id: number) => {
  dialogFormVisible.value = true;
  if (id > 0) {
    dialogTitle.value = "编辑产品";
    getInfo(id);
  } else {
    dialogTitle.value = "添加产品";
  }
};

const handleClose = () => {
  formRef.value.resetFields();
  formData.value = {
    id: 0,
    category: 0,
    part_name: "",
    price: "",
    period: 0,
    status: true,
  };
  dialogFormVisible.value = false;
};

defineExpose({
  dialogOpen,
});
</script>

<style lang="scss">
.el-form-item {
  margin-bottom: 10px;
}
</style>

<style lang="scss" scoped></style>
